package com.kakaxi.mybilibili.module.home.advice.view;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.daimajia.slider.library.SliderLayout;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.AdviceHomeRecyAdapter;
import com.kakaxi.mybilibili.adapter.entity.AdviceHomeMultiItem;
import com.kakaxi.mybilibili.base.BaseFragment;
import com.kakaxi.mybilibili.bean.AdviceHomeItemBean;
import com.kakaxi.mybilibili.bean.BannerItemBean;
import com.kakaxi.mybilibili.bean.EventBusObject;
import com.kakaxi.mybilibili.module.all.WebViewActivity;
import com.kakaxi.mybilibili.module.home.advice.presenter.AdviceHomePresenter;
import com.kakaxi.mybilibili.module.home.advice.presenter.IAdviceHomePresenter;
import com.kakaxi.mybilibili.module.home.find.view.FindCenterActivity;
import com.kakaxi.mybilibili.module.home.find.view.FindRankActivity;
import com.kakaxi.mybilibili.module.video.view.VideoActivity;
import com.kakaxi.mybilibili.utils.CastUncheckedUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 推荐首页面碎片
 * Created by kakaxi on 16/10/11.
 */

public class AdviceHomeFragment extends BaseFragment implements IAdviceHomeView {

    /** 列表 **/
    @BindView(R.id.recv_common_fragment)
    RecyclerView mAdviceHomeRecyView;

    /** 刷新控件 **/
    @BindView(R.id.srl_common_fragment)
    SwipeRefreshLayout mRefreshSRLayout;

    /** 控制层 **/
    private IAdviceHomePresenter mPresenter;

    /** 适配器 **/
    private AdviceHomeRecyAdapter mAdapter;

    public static AdviceHomeFragment newInstance() {
        return new AdviceHomeFragment();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_common_recycler;
    }

    @Override
    protected void setupViews(ViewGroup viewGroup) {
        ButterKnife.bind(this, viewGroup);

        // 初始化刷新控件
        mRefreshSRLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.refreshAdviceHomeData();
            }
        });
        // 初始化列表
        mAdviceHomeRecyView.addOnItemTouchListener(new AdviceHomeOnItemClickListener());
    }

    @Override
    protected void initData() {
        mPresenter = new AdviceHomePresenter(this);
        mPresenter.initAdviceHomeData();
    }

    @Override
    public void showAdviceHomeData(List<AdviceHomeItemBean> adviceHomeItemBeanList,
                                   List<BannerItemBean> bannerItemBeanList) {
        // 适配器数据列表
        List<AdviceHomeMultiItem> multiItemList = new ArrayList<>();
        // 轮播
        AdviceHomeMultiItem bannerItem = new AdviceHomeMultiItem(AdviceHomeMultiItem
                .TYPE_HEADER_BANNER, bannerItemBeanList);
        multiItemList.add(bannerItem);

        // 正文数据转换
        for (int i = 0; i < adviceHomeItemBeanList.size(); i++) {
            AdviceHomeItemBean itemBean = adviceHomeItemBeanList.get(i);
            String type = itemBean.getType();
            if (type.equals("recommend")) { // 推荐
                parseAdviceHomeItem(AdviceHomeMultiItem.TYPE_HEADER_TOP, AdviceHomeMultiItem
                        .TYPE_CONTENT_NORMAL, itemBean, multiItemList);
            } else if (type.equals("live")) { // 直播
                parseAdviceHomeItem(AdviceHomeMultiItem.TYPE_HEADER_LIVE, AdviceHomeMultiItem
                        .TYPE_CONTENT_LIVE, itemBean, multiItemList);
            } else if (type.equals("bangumi_2")) { // 番剧
                parseAdviceHomeItem(AdviceHomeMultiItem.TYPE_HEADER_DRAMA, AdviceHomeMultiItem
                        .TYPE_CONTENT_DRAMA, itemBean, multiItemList);
            } else if (type.equals("weblink")) { // 话题
                parseAdviceHomeItem(AdviceHomeMultiItem.TYPE_HEADER_WEBLINK, AdviceHomeMultiItem
                        .TYPE_CONTENT_WEBLINK, itemBean, multiItemList);
            } else if (type.equals("activity")) { // 活动

            } else { // 通用
                parseAdviceHomeItem(AdviceHomeMultiItem.TYPE_HEADER_NORMAL, AdviceHomeMultiItem
                        .TYPE_CONTENT_NORMAL, itemBean, multiItemList);
            }
        }

        // 设置适配器
        if (mAdapter == null) {
            mAdapter = new AdviceHomeRecyAdapter(multiItemList);
            mAdapter.openLoadAnimation();
            mAdviceHomeRecyView.setAdapter(mAdapter);
        } else {
            mAdapter.setNewData(multiItemList);
        }

    }

    /**
     * 把推荐主页面数据项转换成适配器复杂类型项
     *
     * @param headItemType       标题类型
     * @param bodyItemType       正文类型
     * @param adviceHomeItemBean 主页面数据项
     * @param multiItemList      适配器复杂类型列表
     */
    private void parseAdviceHomeItem(int headItemType, int bodyItemType, AdviceHomeItemBean
            adviceHomeItemBean, List<AdviceHomeMultiItem> multiItemList) {
        // 标题
        AdviceHomeMultiItem headItem = new AdviceHomeMultiItem(headItemType, adviceHomeItemBean
                .getHead());
        multiItemList.add(headItem);
        // 正文
        parseBodyList(bodyItemType, adviceHomeItemBean.getBody(), multiItemList);
    }

    /**
     * 把正文列表转化成适配器复杂类型列表
     *
     * @param itemType         正文类型
     * @param multiItemList    适配器复杂类型列表
     * @param bodyItemBeanList 主页面正文数据项列表
     */
    private void parseBodyList(int itemType, List<AdviceHomeItemBean.AdviceHomeBodyItemBean>
            bodyItemBeanList, List<AdviceHomeMultiItem> multiItemList) {
        for (int i = 0; i < bodyItemBeanList.size(); i++) {
            multiItemList.add(new AdviceHomeMultiItem(itemType, bodyItemBeanList.get(i)));
        }
    }

    @Override
    public void showLoading() {
        mRefreshSRLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mRefreshSRLayout.setRefreshing(false);
    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showMessage(String message) {
        toastHandler(message);
    }

    /**
     * 推荐主页面列表点击监听器
     */
    private class AdviceHomeOnItemClickListener extends OnItemClickListener {

        @Override
        public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int pos) {
            AdviceHomeRecyAdapter adapter = (AdviceHomeRecyAdapter) baseQuickAdapter;
            AdviceHomeMultiItem item = adapter.getItem(pos);
            Object data = item.getData();
            switch (item.getItemType()) {
                case AdviceHomeMultiItem.TYPE_HEADER_BANNER: { // 轮播标题
                    SliderLayout sliderLayout = (SliderLayout) view;
                    List<BannerItemBean> bannerItemBeanList = CastUncheckedUtil.cast(data);
                    BannerItemBean itemBean = bannerItemBeanList.get(sliderLayout
                            .getCurrentPosition());
                    Bundle bundle = new Bundle();
                    bundle.putString(WebViewActivity.EXTRA_URL, itemBean.getLink());
                    goActivity(WebViewActivity.class, bundle);
                    break;
                }
                case AdviceHomeMultiItem.TYPE_HEADER_TOP: { // 推荐标题
                    Bundle bundle = new Bundle();
                    bundle.putInt(FindRankActivity.EXTRA_RANK_TYPE, FindRankActivity.TYPE_NEW);
                    goActivity(FindRankActivity.class, bundle);
                    break;
                }
                case AdviceHomeMultiItem.TYPE_HEADER_LIVE: // 直播标题
                    // 跳转到直播主页面
                    EventBus.getDefault().post(new EventBusObject(EventBusObject
                            .EVENT_MAIN_ACT_TURN_PAGE, 0));
                    break;
                case AdviceHomeMultiItem.TYPE_HEADER_DRAMA: // 番剧标题
                    // 跳转到番剧主页面
                    EventBus.getDefault().post(new EventBusObject(EventBusObject
                            .EVENT_MAIN_ACT_TURN_PAGE, 2));
                    break;
                case AdviceHomeMultiItem.TYPE_HEADER_WEBLINK: { // 话题标题
                    Bundle bundle = new Bundle();
                    bundle.putInt(FindCenterActivity.EXTRA_CENTER_TYPE, FindCenterActivity
                            .TYPE_TOPIC);
                    goActivity(FindCenterActivity.class, bundle);
                    break;
                }
                case AdviceHomeMultiItem.TYPE_HEADER_CENTER: // 活动中心标题
                    break;
                case AdviceHomeMultiItem.TYPE_HEADER_NORMAL: // 通用标题
                    break;
                case AdviceHomeMultiItem.TYPE_CONTENT_LIVE: // 直播正文
                    // 跳转到直播主页面
                    EventBus.getDefault().post(new EventBusObject(EventBusObject
                            .EVENT_MAIN_ACT_TURN_PAGE, 0));
                    break;
                case AdviceHomeMultiItem.TYPE_CONTENT_DRAMA: // 番剧正文
                    break;
                case AdviceHomeMultiItem.TYPE_CONTENT_CENTER: // 活动中心正文
                    break;
                case AdviceHomeMultiItem.TYPE_CONTENT_WEBLINK: { // 话题正文
                    AdviceHomeItemBean.AdviceHomeBodyItemBean itemBean = (AdviceHomeItemBean
                            .AdviceHomeBodyItemBean) data;
                    Bundle bundle = new Bundle();
                    bundle.putString(WebViewActivity.EXTRA_URL, itemBean.getParam());
                    goActivity(WebViewActivity.class, bundle);
                    break;
                }
                case AdviceHomeMultiItem.TYPE_CONTENT_NORMAL: { // 通用正文
                    AdviceHomeItemBean.AdviceHomeBodyItemBean itemBean = (AdviceHomeItemBean
                            .AdviceHomeBodyItemBean) data;
                    Bundle bundle = new Bundle();
                    bundle.putString(VideoActivity.EXTRA_VIDEO_COVER_URL, itemBean.getCover());
                    bundle.putString(VideoActivity.EXTRA_VIDEO_AID, itemBean.getParam());
                    goActivity(VideoActivity.class, bundle);
                    break;
                }
            }
        }
    }

}
